package eu.robico.simulator.simulatorinterface.actionpanel.objects;

import java.awt.Color;
import java.awt.Graphics2D;

import eu.robico.simulator.simulatorinterface.MainController;
import eu.robico.simulator.simulatorinterface.actionpanel.Position;
import eu.robico.simulator.simulatorinterface.communication.Protocol.Trigger;


class CrossRoadsSensor implements RobotComponent
{
	/**
	 * Represents a on-screen crossroads sensor object
	 * @author A.J. Janssen
	 * @version 0.1
	 *
	 */
	private class CrossRoadsSensorObject
	{
		public void paint(Graphics2D graphics) 
		{
			Color sensorColor = (onCrossRoads) ? Color.green : Color.blue;
			graphics.setColor(sensorColor);
			graphics.fillOval((int) x - (size / 2), (int) y - (size / 2), size, size);
		}
	}
	
	private CrossRoadsSensorObject crossRoadsSensorObject;
	private Robot robot;
	private Position centerPosition;
	private int size = 8;
	private double x, y, rotation, distanceToRobotCenter;
	private boolean onCrossRoads = false;
	
	public CrossRoadsSensor(Robot robot, Position location, double rotation)
	{
		this.robot = robot;
		this.x = location.x;
		this.y = location.y;
		this.rotation = rotation;
		
		crossRoadsSensorObject = this.new CrossRoadsSensorObject();
	}
	
	//RobotComponent implementation below
	
	@Override
	public void update()
	{
		
		for (ActionPanelObject object : robot.getActionPanel().getObjects())
			if (object instanceof CrossRoadsObject && ((CrossRoadsObject) object).containsPosition(centerPosition))
			{
				if (!onCrossRoads)
				{
					//Send toggle trigger
					//TODO: send trigger here
					MainController.getInstance().sendTrigger(Trigger.CROSSROADSTOGGLE);
					onCrossRoads = true;
				}
				
				return;
			}
		//No collisions detected
		if (onCrossRoads)
		{
			//TODO: send trigger here
			MainController.getInstance().sendTrigger(Trigger.CROSSROADSTOGGLE);
			onCrossRoads = false;
		}
		
		
	}
	
	@Override
	public void paint(Graphics2D graphics) 
	{
		crossRoadsSensorObject.paint(graphics);
	}

	@Override
	public double getRotation() 
	{
		return rotation;
	}

	@Override
	public void setRotation(double rotation) 
	{
		this.rotation = rotation;
	}

	@Override
	public void setTrueCenter(Position centerPoint) 
	{
		this.centerPosition = centerPoint;
	}

	@Override
	public Position getTrueCenter() 
	{
		return centerPosition;
	}

	@Override
	public double getX() 
	{
		return x;
	}

	@Override
	public double getY() 
	{
		return y;
	}

	@Override
	public void setX(double x) 
	{
		this.x = x;
	}

	@Override
	public void setY(double y) 
	{
		this.y = y;
	}

	@Override
	public RobotComponentType getType() 
	{
		return RobotComponentType.ColorLaneSensor;
	}

	@Override
	public Position getPosition() 
	{
		return new Position(x, y);
	}

	@Override
	public double getDistanceToRobotCenter() 
	{
		return distanceToRobotCenter;
	}

	@Override
	public void setDistanceToRobotCenter(double distanceToRobotCenter) 
	{
		this.distanceToRobotCenter = distanceToRobotCenter;
	}
}
